package com.hyst.interceptor;

import com.hyst.common.Contants;
import com.hyst.domain.Admin;
import com.hyst.service.AdminService;
import com.hyst.utils.CookieUtil;
import com.hyst.utils.RSAUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/***
 * @Des:
 * @Author: jiyang
 * @Date: 2018-10-26 23:01
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private AdminService adminService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
        try {
            String path = request.getContextPath();
            String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";

            if (request.getRequestURI().contains("SMSverification") || request.getRequestURI().contains("phone") || request.getRequestURI().contains("freeAuditions")) {
                return true;
            }
            autoLogin(request);
            System.out.println(request.getRequestURI());

            Object obj = request.getSession().getAttribute(Contants.SESSION_USER_INFO);
            if (obj != null && obj instanceof Admin) {
                return true;
            }
            if (request.getRequestURI().contains("/login")) {
                return true;
            }

            if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
                PrintWriter printWriter = response.getWriter();
                response.setStatus(499);
                printWriter.print(basePath + "login");
                return false;
            } else {
                response.sendRedirect(basePath + "login");
                return false;
            }
        } catch (Exception e){

        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    private void autoLogin(HttpServletRequest request) {
        //session
        Object obj = request.getSession().getAttribute(Contants.SESSION_USER_INFO);
        if (obj != null && obj instanceof Admin) {
            return;
        }

        //cookie
        String uuid = CookieUtil.getCookieValue(request, Contants.SESSION_NAME);
        String name = CookieUtil.getCookieValue(request, Contants.COOKIE_LOGIN_NAME);
        String pwd = CookieUtil.getCookieValue(request, Contants.COOKIE_LOGIN_PWD);

        if (StringUtils.isNotBlank(uuid) && StringUtils.isNotBlank(name) && StringUtils.isNotBlank(pwd)) {
            Admin admin = adminService.getAdmin(name);
            try {
                pwd = pwd.replace(" ", "+");
//                pwd = RSAUtil.base64Encode(RSAUtil.privateKeyDecodeRSA(pwd));
                if (admin != null && pwd.equals(admin.getPassword())) {
                    request.getSession().setAttribute(Contants.SESSION_USER_INFO, admin);
                    return;
                }
            } catch (Exception e) {
                return;
            }

        }
    }
}
